package com.learning.spark.java.core.wordcount;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;

/**
 * ClassName: WordCount
 * Description: WordCount 简单例子
 * Date: 2018/6/13 15:49 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class WordCount {

    public static void main(String[] args) {

        wordCount1();
    }


    private static void wordCount1() {

        SparkConf conf = new SparkConf().setMaster("local").setAppName("WordCount_Java");
        JavaSparkContext context = new JavaSparkContext(conf);

        // 获取文件
        JavaRDD<String> text = context.textFile("wordCount.txt");

        // 相当于 Splitting  操作
        JavaRDD<String> words = text.flatMap(new FlatMapFunction<String, String>() {
            public Iterable<String> call(String line) {
                return Arrays.asList(line.split(" "));
            }
        });

        // 相当于 Mapping 操作
        JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
            public Tuple2<String, Integer> call(String word) throws Exception {
                return new Tuple2<String, Integer>(word, 1);
            }
        });

        // 相当于 Shuffling 操作 + Reducing
        JavaPairRDD<String, Integer> result = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
            public Integer call(Integer value1, Integer value2) throws Exception {
                return value1 + value2;
            }
        });
        // 按照 Key 排序
        result.sortByKey(false);
        result.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            public void call(Tuple2<String, Integer> tuple) throws Exception {
                System.out.println(tuple._1 + " " + tuple._2);
            }
        });

        // 处理按照 value 排序
        // 1- key-value 先换个位置
        JavaPairRDD<Integer, String> temp = result.mapToPair(new PairFunction<Tuple2<String, Integer>, Integer, String>() {
            public Tuple2<Integer, String> call(Tuple2<String, Integer> tuple) throws Exception {
                return new Tuple2<Integer, String>(tuple._2, tuple._1);
            }
        });
        // 2- 排好序
        temp.sortByKey();

        // 3- key-value 再次换个位置
        JavaPairRDD<String, Integer> sorted = temp.mapToPair(new PairFunction<Tuple2<Integer, String>, String, Integer>() {
            public Tuple2<String, Integer> call(Tuple2<Integer, String> tuple) throws Exception {
                return new Tuple2<String, Integer>(tuple._2, tuple._1);
            }
        });

        sorted.foreach(new VoidFunction<Tuple2<String, Integer>>() {
            public void call(Tuple2<String, Integer> tuple) throws Exception {
                System.out.println(tuple._1 + " " + tuple._2);
            }
        });

        context.close();
    }


}
